package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.BaseContext;
import com.itheima.youdianda.dto.*;
import com.itheima.youdianda.entity.Hot;
import com.itheima.youdianda.entity.YddArticle;
import com.itheima.youdianda.entity.YddCate;
import com.itheima.youdianda.entity.YddUserData;
import com.itheima.youdianda.mapper.YddArticleMapper;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import com.itheima.youdianda.service.YddUserDataService;
import com.itheima.youdianda.util.MyPages;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {

    @Autowired
    private YddCateService yddCateService;

    @Autowired
    private YddArticleMapper yddArticleMapper;

    @Autowired
    private YddUserDataService yddUserDataService;

    @Autowired
    private YddArticleService yddArticleService;


    @Override
    public HotDto getHot() {
//        List<List<Hot>> hotLists = new ArrayList<>();
//        List<YddCate> cateList = yddCateService.list();
//        List<Hot> hotList = new ArrayList<>();
/*        for (YddCate cate : cateList) {
            Integer id = cate.getId();
            LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YddArticle::getCateid, id);
            wrapper.eq(YddArticle::getIshot, 1);
            List<YddArticle> list = this.list(wrapper);
            for (YddArticle yddArticle : list) {
                Hot hot = new Hot();
                hot.setCatename(cate.getCatename());
                hot.setId(yddArticle.getId());
                hot.setLovenum(yddArticle.getLovenum());
                hot.setPic(yddArticle.getPic());
                hot.setTitle(yddArticle.getTitle());
                hotList.add(hot);
            }
            hotLists.add(hotList);
//            YddArticle one = this.getOne(wrapper);
        }*/
//        HotDto hotDto = new HotDto();
//        hotDto.setList(hotLists);
//        hotDto.setDesc("侧边最新文章");
//        return hotDto;
		List<Hot> hotList = new ArrayList<>();
		List<YddCate> cateList = yddCateService.list();
        for (YddCate cate : cateList) {
			Hot hot = new Hot();
			Integer id =cate.getId();
			LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(YddArticle::getCateid, id);
			wrapper.eq(YddArticle::getIshot, 1);
			List<YddArticle> list = this.list(wrapper);
            for (YddArticle yddArticle : list) {
                hot.setCatename(cate.getCatename());
                hot.setId(yddArticle.getId());
                hot.setLovenum(yddArticle.getLovenum());
                hot.setPic(yddArticle.getPic());
                hot.setTitle(yddArticle.getTitle());
            }
//			hot.setCatename(cate.getCatename());
//			hot.setId(list.get(i).getId());
//			hot.setLovenum(list.get(i).getLovenum());
//			hot.setPic(list.get(i).getPic());
//			hot.setTitle(list.get(i).getTitle());
			hotList.add(hot);
        }

//		for (YddCate cate : cateList) {
//			Hot hot = new Hot();
//			Integer id = cate.getId();
//			LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
//			wrapper.eq(YddArticle::getCateid, id);
//			wrapper.eq(YddArticle::getIshot, 1);
//			List<YddArticle> list = this.list(wrapper);
//			hot.setCatename(cate.getCatename());
//			hot.setId(list.get(1).getId());
//			hot.setLovenum(list.get(1).getLovenum());
//			hot.setPic(list.get(1).getPic());
//			hot.setTitle(list.get(1).getTitle());
//			hotList.add(hot);
//		}

        HotDto hotDto = new HotDto();
        hotDto.setList(hotList);
        hotDto.setDesc("侧边最新文章");
        return hotDto;
    }

    @Override
    public MyPages<YddArticle> getMyArticleByPage(String page, String limit) {
        Page<YddArticle> page1 = new Page<>(Long.parseLong(page), Long.parseLong(limit));
        // 获取当前登录用户id
        Long currentId = BaseContext.getCurrentId();
        // 自定义page对象,用于返回前端
        MyPages<YddArticle> myPage = new MyPages<>();
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getUser_id, currentId).orderByDesc(YddArticle::getCreate_date);
        this.page(page1, wrapper);
        List<YddArticle> myRecords = page1.getRecords();

        myPage.setCount(page1.getTotal());
        myPage.setTotalPages(page1.getPages());
        myPage.setPageSize(page1.getSize());
        myPage.setCurrentPage(page1.getCurrent());
        myPage.setData(myRecords);
        return myPage;
    }

    @Override
    public Map<String, Object> show(String id) {
        Map<String, Object> map = new HashMap<>();

        List<YddArticle> prev = new ArrayList<>();
        List<YddArticle> next = new ArrayList<>();

        // -------prev & next-------
        // 上一条数据
        YddArticle prevArticle = yddArticleMapper.getPrev(id);
        // 下一条数据
        YddArticle nextArticle = yddArticleMapper.getNext(id);

        // 当数据不为null时,才向集合中放
        if (prevArticle != null) {
            prev.add(prevArticle);
        }
        if (nextArticle != null) {
            next.add(nextArticle);
        }
        // -------prev & next-------

        // -------info--------
        YddArticle yddArticle = this.getById(id);
        ArticleInfoDto info = new ArticleInfoDto();
        BeanUtils.copyProperties(yddArticle, info);
        YddCate yddCate = yddCateService.getById(yddArticle.getCateid());
        info.setCatename(yddCate.getCatename());
        // 收藏
        LambdaQueryWrapper<YddUserData> collectWrapper = new LambdaQueryWrapper<>();
        collectWrapper.eq(YddUserData::getArticle_id, yddArticle.getId())
                .eq(YddUserData::getUser_id, yddArticle.getUser_id())
                .eq(YddUserData::getType, 1);
        // 点赞
        LambdaQueryWrapper<YddUserData> likeWrapper = new LambdaQueryWrapper<>();
        likeWrapper.eq(YddUserData::getArticle_id, yddArticle.getId())
                .eq(YddUserData::getUser_id, yddArticle.getUser_id())
                .eq(YddUserData::getType, 2);
        boolean isLike = yddUserDataService.getOne(likeWrapper) != null;
        boolean isCollect = yddUserDataService.getOne(collectWrapper) != null;
        info.setIsCollect(isCollect);
        info.setIsLike(isLike);
        // -------info--------

        // -------recommend------
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getIshot, 1);
        List<YddArticle> articleList = this.list(wrapper);
        List<ArticleInfoDto> recommend = new ArrayList<>();
        for (YddArticle article : articleList) {
            ArticleInfoDto articleInfoDto = new ArticleInfoDto();
            BeanUtils.copyProperties(article, articleInfoDto);
            String catename = yddCateService.getById(article.getCateid()).getCatename();
            articleInfoDto.setCatename(catename);
            recommend.add(articleInfoDto);
            if (recommend.size() >= 8) {
                break;
            }
        }
        // -------recommend------

        map.put("prev", prev);
        map.put("next", next);
        map.put("info", info);
        map.put("recommend", recommend);
        return map;
    }

    @Override
    public void userDataHandle(String type, String action, String article_id) {
        Long currentId = BaseContext.getCurrentId();
        YddArticle article = yddArticleService.getById(article_id);

        // 取消收藏/点赞
        if ("del".equals(action)) {
            LambdaQueryWrapper<YddUserData> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YddUserData::getUser_id, currentId)
                    .eq(YddUserData::getArticle_id, article_id)
                    .eq(YddUserData::getType, type);
            yddUserDataService.remove(wrapper);
            if ("1".equals(type)) {
                // 对应文章收藏数量 -1
                article.setLovenum(String.valueOf(Integer.parseInt(article.getLovenum()) - 1));
                yddArticleService.save(article);
            }
        } else {
            // 收藏/点赞
            YddUserData yddUserData = new YddUserData();
            yddUserData.setUser_id(Integer.parseInt(String.valueOf(currentId)));
            yddUserData.setArticle_id(Integer.parseInt(article_id));
            yddUserData.setType(Integer.parseInt(type));
            yddUserDataService.save(yddUserData);
            if ("1".equals(type)) {
                // 对应文章收藏数量 +1
                article.setLovenum(String.valueOf(Integer.parseInt(article.getLovenum()) + 1));
                yddArticleService.save(article);
            }
        }
    }

    @Override
    public NewListDto getNewList(String page, String limit) {
        NewListDto newListDto = new NewListDto();
        Page<YddArticle> pageInfo = new Page<>(Long.parseLong(page), Long.parseLong(limit));
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getIstop, 1);
        yddArticleService.page(pageInfo, wrapper);
        List<YddArticle> records = pageInfo.getRecords();
        newListDto.setData(records);
        newListDto.setCount((int) pageInfo.getTotal());
        newListDto.setPageSize((int) pageInfo.getSize());
        newListDto.setCurrentPage((int) pageInfo.getCurrent());
        newListDto.setTotalPages((int) pageInfo.getPages());
        return newListDto;
    }

    @Override
    public DataListDto getDataList(String page, String limit, String cateid) {
        DataListDto dataListDto = new DataListDto();
        Page<YddArticle> pageInfo = new Page<>(Long.parseLong(page), Long.parseLong(limit));
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getCateid, cateid);
        yddArticleService.page(pageInfo, wrapper);
        YddCate cate = yddCateService.getById(cateid);
        String catename = cate.getCatename();
        List<DataDto> list = new ArrayList<>();
        List<YddArticle> records = pageInfo.getRecords();
        for (YddArticle record : records) {
            DataDto dataDto = new DataDto();
            BeanUtils.copyProperties(record, dataDto);
            dataDto.setCatename(catename);
            list.add(dataDto);
        }
        dataListDto.setData(list);
        dataListDto.setCount((int) pageInfo.getTotal());
        dataListDto.setPageSize((int) pageInfo.getSize());
        dataListDto.setCurrentPage((int) pageInfo.getCurrent());
        dataListDto.setTotalPages((int) pageInfo.getPages());
        return dataListDto;
    }


}
